package com.dbb.trade.entity.product;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.IndexColumn;

import com.dbb.trade.entity.IdEntity;
import com.dbb.trade.entity.status.SuitType;

@Entity
public class Suit extends IdEntity {
	private List<ColorSize> colorSizes;
	private SuitType type;
	
	public Suit(SuitType type) {
		this.type = type;
	}

	public SuitType getType() {
		return type;
	}
	
	public void setType(SuitType type) {
		this.type = type;
	}
	
	@Transient
	public Integer getColorSizesPrs() {
		Integer prs = 0;
		for(ColorSize cs : colorSizes) {
			prs += cs.getNum();
		}
		return prs;
	}

	@ElementCollection
	@JoinTable(name = "COLOR_SIZE", joinColumns = {@JoinColumn(name = "SUIT_ID")})
	@IndexColumn(name = "linenum")
	public List<ColorSize> getColorSizes() {
		return colorSizes;
	}

	public void setColorSizes(List<ColorSize> colorSizes) {
		this.colorSizes = colorSizes;
	}

	public void setColorSizes(List<Color> colors, List<Size> sizes) {
		if(this.colorSizes == null) {
			colorSizes = new ArrayList<ColorSize>();
		}
		for(Color c : colors) {
			for(Size s : sizes) {
				colorSizes.add(new ColorSize(c,s));
			}
		}
	}
	@Transient
	public void setNums(Integer ...nums) {
		int size = nums.length > colorSizes.size() ? colorSizes.size() : nums.length;
		for(int i = 0; i < size; i++) {
			colorSizes.get(i).setNum(nums[i]);
		}
	}
	@Transient
	public Integer getTotalPairs() {
		Integer pairs = 0;
		for(ColorSize cs : colorSizes) {
			pairs += cs.getNum();
		}
		return pairs;
	}
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
